package org.example.musicproject.dao.impl;

import org.example.musicproject.dao.CommentsDao;
import org.example.musicproject.entity.Comments;
import org.example.musicproject.util.DButil;

import java.sql.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 创建人:黄庭萍
 * 创建时间: 2024/12/10 20:45
 * 创建类型: CommentsDaoImpl
 */
public class CommentsDaoImpl implements CommentsDao {

    @Override
    public List<Comments> all() {
        String sql = "select id AS cid ,content,comment,delReason from comments";
        System.out.println("=====查询所有歌手信息============");
        System.out.println("sql="+sql);
        System.out.println("================================");
        return DButil.list(sql, Comments.class);
    }

    @Override
    public Comments byId(int id) {
        String sql = "select id AS cid ,content,comment,delReason from comments where id = ?";
        System.out.println("=====根据id查询歌手信息============");
        System.out.println("sql="+sql);
        System.out.println("id="+id);
        System.out.println("================================");
        return DButil.one(sql, Comments.class, id);
    }

    @Override
    public int add(Comments comments) {
        String sql="insert into singer(content,comment) values(?,?)";
        System.out.println("=====根据id添加评论信息========");
        System.out.println("sql=" + sql);
        System.out.println("comments = " + comments);
        System.out.println("========================");
        return DButil.update(sql, comments);
    }

    @Override
    public int updateDelReason(int cid, String delReason) {
        String sql = "UPDATE comments SET delReason = ? WHERE id = ?";
        System.out.println("=====更新评论删除原因========");
        System.out.println("sql=" + sql);
        System.out.println("id=" + cid);
        System.out.println("delReason = " + delReason);
        System.out.println("========================");

        return DButil.update(sql, delReason, cid);
    }

    @Override
    public List<Comments> getCommentsBySongId(int soid) {
        String sql = "SELECT c.id AS cid, content,comment,delReason FROM comments c JOIN commentAddsongs cs ON c.id = cs.cid WHERE cs.soid = 3";
        System.out.println("=====根据歌曲评论关系绑定表查询评论信息========");
        System.out.println("sql=" + sql);
        System.out.println("soid=" + soid);
        System.out.println("========================");
        List<Comments> list = DButil.list(sql, Comments.class);
        return list;
    }

    @Override
    public boolean delete(int cid,String delReason) {
        String sql = "DELETE FROM comments WHERE id = ?";
        System.out.println("=====根据id删除评论信息========");
        System.out.println("sql=" + sql);
        System.out.println("id=" + cid);
        System.out.println("========================");
        Connection conn = null;
        try (PreparedStatement ps = conn.prepareStatement(sql)) {

            ps.setInt(1, cid);
            ps.setString(2, delReason);
            int rows = ps.executeUpdate();

            if (rows > 0) {
                System.out.println("评论已删除, ID: " + cid);
                return true;
            } else {
                System.out.println("没有找到ID为 " + cid + " 的评论以删除");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

//
//    @Override
//    public int add(Connection conn, int soid, int cid) {
//        return 0;
//    }

//    @Override
//    public int update(int id, String delReason) {
//        return 0;
//    }
}
